﻿using System.Runtime.InteropServices;
using System.Text;

namespace _59_C_怎样读取ini文件Section节点的所有键值对
{
    internal class Program
    {
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string ky, string defalutval, string filePath);
        [DllImport("kernel32")]
        private static extern bool GetPrivateProfileString(string section, string ky, string defalutval, StringBuilder stringbuilder, int size, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string section, string key, int defaultVal, string filePath);
        [DllImport("kernel32")]//读取section所有的键值对
        private static extern bool GetPrivateProfileSection(string section, byte[] bytes, int size, string filePath);
        [DllImport("kernel32")]//读取所有section
        private static extern bool GetPrivateProfileSectionNames(byte[] bytes, int size, string filePath);

        private static string rootPath = ".\\test.ini";
        private static string root = "C:\\Users\\nengjupc.mei\\Desktop\\CM.ini";
        static void Main(string[] args)
        {
            //WriteIni();
            //ReadIni();
            //DeleteKey(null);
            //ReadAllKeyValues("Student");
            ReadAllSections();

            Console.WriteLine("运行成功！");
            Console.ReadKey();
        }
        private static void ReadAllKeyValues(string section)
        {

            byte[] bytes = new byte[2048];
            bool bl = GetPrivateProfileSection(section, bytes, bytes.Length, rootPath);
            if (bl)
            {
                string str = Encoding.Default.GetString(bytes).TrimEnd('\0');
                string[] strings = str.Split(new Char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in strings)
                {
                    Console.WriteLine(s);
                }
            }
        }
        static void ReadAllSections()
        {
            byte[] bytes = new byte[2048];
            bool b1 = GetPrivateProfileSectionNames(bytes, bytes.Length, root);
            if (b1)
            {
                string str = Encoding.Default.GetString(bytes).TrimEnd('\0');
                string[] strings = str.Split(new Char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in strings)
                {
                    Console.WriteLine(s);
                }
            }
        }

        private static void WriteIni()
        {
            WritePrivateProfileString("Student", "Name", "Name1", rootPath);
            WritePrivateProfileString("Student", "Grade", "Grade1", rootPath);
            WritePrivateProfileString("Student", "Age", "18", rootPath);
        }

        private static void ReadIni()
        {
            StringBuilder stringbuilder = new StringBuilder();
            GetPrivateProfileString("Student", "Name", "", stringbuilder, stringbuilder.Capacity, rootPath);
            Console.WriteLine(stringbuilder);

            int age = GetPrivateProfileInt("Student", "Age", 0, rootPath);
            Console.WriteLine(age);
            Console.WriteLine();
        }

        private static void DeleteKey()
        {
            WritePrivateProfileString("Student", "Name", null, rootPath);
        }

        private static void DeleteKey1()
        {
            WritePrivateProfileString("Student", null, null, rootPath);
        }

    }
}
